package org.lanqiao.ssm.common.quartz.nativeTest;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzCronTest {

	public static void main(String[] args) throws Throwable {
		SchedulerFactory factory = new StdSchedulerFactory();
		Scheduler scheduler = factory.getScheduler();

		JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("job1", "group1").build();
		// @NOTICE
		// 与SimpleTrigger对比：类不同了，现在的是Trigger的子类CronTrigger；withSchedule中的参数变为CronScheduleBuilder了

		// SimpleTrigger simpleTrigger = new
		// SimpleTrigger("trigger1_1","tgroup1");
		// simpleTrigger.setStartTime(new Date());//开始运行时间
		// simpleTrigger.setRepeatInterval(2000); //运行间隔单位为毫秒
		// simpleTrigger.setRepeatCount(100); //运行次数
		// simpleTrigger.setJobGroup("j_group1");
		// simpleTrigger.setJobName("j_job1");
		// CronScheduleBuilder可以通过类似"0/13 * * * * ?"这种表达式来创建定时任务
		// 当前这个表达式的定义是每个秒是13的倍数，或者是0的时候，都触发任务
		CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
				.withSchedule(CronScheduleBuilder.cronSchedule("0/13 * * * * ?")).build();

		scheduler.scheduleJob(job, trigger);

		scheduler.start();
		try {
			// 等待60秒查看效果
			Thread.sleep(60L * 1000L);
		} catch (Exception e) {
		}
		scheduler.shutdown(true);
	}

}
